<?php
 
/* setting zona waktu */
date_default_timezone_set('Asia/Jakarta');
 
/* konstruktor halaman pdf sbb :    
   P  = Orientasinya "Potrait"
   cm = ukuran halaman dalam satuan centimeter
   A4 = Format Halaman
    
   jika ingin mensetting sendiri format halamannya, gunakan array(width, height)  
   contoh : $this->fpdf->FPDF("P","cm", array(20, 20));  
*/
 
$this->fpdf->FPDF("L","cm","A4");
 
// kita set marginnya dimulai dari kiri, atas, kanan. jika tidak diset, defaultnya 1 cm
$this->fpdf->SetMargins(0.3,1,0.5);
 $this->fpdf->SetAutoPageBreak(false, 0.5);
 
/* AliasNbPages() merupakan fungsi untuk menampilkan total halaman
   di footer, nanti kita akan membuat page number dengan format : number page / total page
*/
$this->fpdf->AliasNbPages();
 
// AddPage merupakan fungsi untuk membuat halaman baru
$this->fpdf->AddPage();
 
// Setting Font : String Family, String Style, Font size 
$this->fpdf->SetFont('Arial','B',12);
 
/* Kita akan membuat header dari halaman pdf yang kita buat 
   -------------- Header Halaman dimulai dari baris ini -----------------------------
*/
$this->pdf_header();
 
/* -------------- Header Halaman selesai ------------------------------------------------*/
 
$this->fpdf->Ln(1);
$this->fpdf->SetFont('Arial','B',12);
$this->fpdf->Cell(28,1,'DAILY SALES REPORT',0,0,'C');
$this->fpdf->Ln();
$this->fpdf->Cell(28,1,$date,0,0,'C');
 
 
/* setting header table */
$this->fpdf->Ln(1);
$x = $this->fpdf->GetX();
$y = $this->fpdf->GetY();
$this->fpdf->SetXY($x, $y);
$this->fpdf->SetFont('Arial','B',8);
$this->fpdf->SetFillColor(216,216,216);
$this->fpdf->Multicell(0.8 , 0.6, "No\n " , 1,  'C', true);
$this->fpdf->SetXY($x+0.8, $y);
$this->fpdf->Multicell(2.5 , 0.6, "Check In\n " , 1,  'C', true);
$this->fpdf->SetXY($x+3.3, $y);
$this->fpdf->Multicell(2.5 , 0.6, "Check Out\n " , 1,  'C', true);
$this->fpdf->SetXY($x+5.8, $y);
$this->fpdf->Multicell(1 , 0.6, "Room\n " , 1,  'C', true);
$this->fpdf->SetXY($x+6.8, $y);
$this->fpdf->Multicell(1.5 , 0.6, 'Room Type' , 1,  'C', true);
$this->fpdf->SetXY($x+8.3, $y);
$this->fpdf->Multicell(2 , 0.6, "Name\n " , 1,  'C', true);
$this->fpdf->SetXY($x+10.3, $y);
$this->fpdf->Multicell(2 , 0.6, "Nationality\n " , 1,  'C', true);
$this->fpdf->SetXY($x+12.3, $y);
$this->fpdf->Multicell(1 , 0.6, "PAX\n " , 1,  'C', true);
$this->fpdf->SetXY($x+13.3, $y);
$this->fpdf->Multicell(3 , 0.6, "Service / Housekeeping " , 1,  'C', true);
$this->fpdf->SetXY($x+16.3, $y);
$this->fpdf->Multicell(2 , 0.6, "Qty / UoM\n " , 1,  'C', true);
$this->fpdf->SetXY($x+18.3, $y);
$this->fpdf->Multicell(2.5 , 0.6, "Price\n " , 1,  'C', true);
$this->fpdf->SetXY($x+20.8, $y);
$this->fpdf->Multicell(2 , 0.6, "Disc\n " , 1,  'C', true);
$this->fpdf->SetXY($x+22.8, $y);
$this->fpdf->Multicell(1 , 0.6, "Serv Tax" , 1, 'C', true);
$this->fpdf->SetXY($x+23.8, $y);
$this->fpdf->Multicell(1 , 0.6, 'PPn Tax' , 1,  'C', true);
$this->fpdf->SetXY($x+24.8, $y);
$this->fpdf->Multicell(1.8 , 0.6, 'Payment Type' , 1,  'C', true);
$this->fpdf->SetXY($x+26.6, $y);
$this->fpdf->Multicell(2.5 , 0.6, "Total \n " , 1,  'C', true);
$this->fpdf->SetXY($x, $y+0.6);
$no = 1;
$grand_total = 0;
$ids = array();
/* generate hasil query disini */
foreach($rooms as $row) {
	
	$this->fpdf->Ln();
	$x = $this->fpdf->GetX();
	$y = $this->fpdf->GetY();
	$this->fpdf->SetXY($x, $y);
	$this->fpdf->SetFont('Arial','',7);
	$this->fpdf->Cell(0.8 , 0.6, $no++ , 1, 'LR', 'C');
	$this->fpdf->Cell(2.5 , 0.6, $row['check_in_time'] , 1, 'LR', 'C');
	$this->fpdf->Cell(2.5 , 0.6, $row['check_out_time'] , 1, 'LR', 'C');
	$this->fpdf->Cell(1 , 0.6, $row['room_number'] , 1, 'LR', 'C');
	$this->fpdf->Cell(1.5 , 0.6, $row['room_type_name'] , 1, 'LR', 'C');
	$this->fpdf->Cell(2 , 0.6, $row['guest_name'] , 1, 'LR', 'C');
	$this->fpdf->Cell(2 , 0.6, $row['nationality'] , 1, 'LR', 'C');
	$this->fpdf->Cell(1 , 0.6, $row['resv_adult_number']+$row['resv_child_number']+$row['resv_infant_number'] , 1, 'LR', 'C');
	$this->fpdf->Cell(3 , 0.6, '' , 1, 'LR', 'C');
	$this->fpdf->Cell(2 , 0.6, '' , 1, 'LR', 'C');
	$this->fpdf->Cell(2.5 , 0.6, 'Rp. '.number_format($row['price'],2,',','.') , 1, 'LR', 'R');
	$total = $row['price'];
	if($row['discount_percent'] != null){
		$this->fpdf->Cell(2 , 0.6, number_format($row['discount_percent'],2,',','.').' %' , 1, 'LR', 'R');
		$total = $total + ($total * $row['discount_percent'] / 100);
	}
	else if($row['discount_nominal'] != null){
		$this->fpdf->Cell(2 , 0.6, 'Rp. '.number_format($row['discount_nominal'],2,',','.') , 1, 'LR', 'R');
		$total = $total + $row['discount_nominal'];
	}
	else {
		$this->fpdf->Cell(2 , 0.6, '' , 1, 'LR', 'L');
	}
	$this->fpdf->Cell(1 , 0.6, number_format($row['service_tax'],2,',','.').'%' , 1, 'LR', 'R');
	$this->fpdf->Cell(1 , 0.6, number_format($row['ppn_tax'],2,',','.').'%' , 1, 'LR', 'R');
	$this->fpdf->Cell(1.8 , 0.6, $row['payment_name'] , 1, 'LR', 'C');
	$total = $total + ($total * $row['service_tax'] / 100) + ($total * $row['ppn_tax'] / 100);
	$this->fpdf->Cell(2.5 , 0.6, 'Rp. '.number_format($total,2,',','.') , 1, 'LR', 'R');
	$grand_total = $grand_total + $total;
	
	if($this->fpdf->GetY() >= 17.5) {
		$this->pdf_footer();
		$this->fpdf->AddPage('L', 'A4');
		$this->pdf_header();
	}
	
	if(!in_array($row['resv_id'], $ids)) {
		$ids[] = $row['resv_id'];
		
		foreach($row['services'] as $row2) {
			$this->fpdf->Ln();
			$x = $this->fpdf->GetX();
			$y = $this->fpdf->GetY();
			$this->fpdf->SetXY($x, $y);
			$this->fpdf->SetFont('Arial','',7);
			$this->fpdf->Cell(0.8 , 0.6, '' , 1, 'LR', 'C');
			$this->fpdf->Cell(2.5 , 0.6, '' , 1, 'LR', 'C');
			$this->fpdf->Cell(2.5 , 0.6, '' , 1, 'LR', 'C');
			$this->fpdf->Cell(1 , 0.6, '' , 1, 'LR', 'C');
			$this->fpdf->Cell(1.5 , 0.6, '' , 1, 'LR', 'C');
			$this->fpdf->Cell(2 , 0.6, '' , 1, 'LR', 'C');
			$this->fpdf->Cell(2 , 0.6, '' , 1, 'LR', 'C');
			$this->fpdf->Cell(1 , 0.6, '' , 1, 'LR', 'C');
			$this->fpdf->Cell(3 , 0.6, $row2['service_name'] , 1, 'LR', 'C');
			$this->fpdf->Cell(2 , 0.6, number_format($row2['serv_det_qty'],2,',','.').' / '.$row2['service_uom'] , 1, 'LR', 'C');
			$this->fpdf->Cell(2.5 , 0.6, 'Rp. '.number_format($row2['serv_det_price'],2,',','.') , 1, 'LR', 'R');
			$total = $row2['serv_det_price'] * $row2['serv_det_qty'];
			if($row2['discount_percent'] != null){
				$this->fpdf->Cell(2 , 0.6, number_format($row2['discount_percent'],2,',','.').' %' , 1, 'LR', 'R');
				$total = $total - ($total * $row2['discount_percent'] / 100);
			}
			else if($row2['discount_nominal'] != null){
				$this->fpdf->Cell(2 , 0.6, 'Rp. '.number_format($row2['discount_nominal'],2,',','.') , 1, 'LR', 'R');
				$total = $total - $row2['discount_nominal'];
			}
			else {
				$this->fpdf->Cell(2 , 0.6, '' , 1, 'LR', 'L');
			}
			$this->fpdf->Cell(1 , 0.6, number_format($row2['service_tax'],2,',','.').'%' , 1, 'LR', 'R');
			$this->fpdf->Cell(1 , 0.6, number_format($row2['ppn_tax'],2,',','.').'%' , 1, 'LR', 'R');
			$this->fpdf->Cell(1.8 , 0.6, '' , 1, 'LR', 'C');
			$total = $total + ($total * $row2['service_tax'] / 100) + ($total * $row2['ppn_tax'] / 100);
			$this->fpdf->Cell(2.5 , 0.6, 'Rp. '.number_format($total,2,',','.') , 1, 'LR', 'R');
			$grand_total = $grand_total + $total;
			
			if($this->fpdf->GetY() >= 17.5) {
				$this->pdf_footer();
				$this->fpdf->AddPage('L', 'A4');
				$this->pdf_header();
			}
		}
	}
	
	foreach($row['materials'] as $row2) {
		$this->fpdf->Ln();
		$x = $this->fpdf->GetX();
		$y = $this->fpdf->GetY();
		$this->fpdf->SetXY($x, $y);
		$this->fpdf->SetFont('Arial','',7);
		$this->fpdf->Cell(0.8 , 0.6, '' , 1, 'LR', 'C');
		$this->fpdf->Cell(2.5 , 0.6, '' , 1, 'LR', 'C');
		$this->fpdf->Cell(2.5 , 0.6, '' , 1, 'LR', 'C');
		$this->fpdf->Cell(1 , 0.6, '' , 1, 'LR', 'C');
		$this->fpdf->Cell(1.5 , 0.6, '' , 1, 'LR', 'C');
		$this->fpdf->Cell(2 , 0.6, '' , 1, 'LR', 'C');
		$this->fpdf->Cell(2 , 0.6, '' , 1, 'LR', 'C');
		$this->fpdf->Cell(1 , 0.6, '' , 1, 'LR', 'C');
		$this->fpdf->Cell(3 , 0.6, $row2['material_name'] , 1, 'LR', 'C');
		$this->fpdf->Cell(2 , 0.6, number_format($row2['resv_mat_qty'],2,',','.').' / '.$row2['material_uom'] , 1, 'LR', 'C');
		$this->fpdf->Cell(2.5 , 0.6, 'Rp. '.number_format($row2['resv_mat_price'],2,',','.') , 1, 'LR', 'R');
		$total = $row2['resv_mat_price'] * $row2['resv_mat_qty'];
		if($row['discount_percent'] != null){
			$this->fpdf->Cell(2 , 0.6, number_format($row['discount_percent'],2,',','.').' %' , 1, 'LR', 'R');
			$total = $total - ($total * $row['discount_percent'] / 100);
		}
		else if($row['discount_nominal'] != null){
			$this->fpdf->Cell(2 , 0.6, 'Rp. '.number_format($row['discount_nominal'],2,',','.') , 1, 'LR', 'R');
			$total = $total - $row['discount_nominal'];
		}
		else {
			$this->fpdf->Cell(2 , 0.6, '' , 1, 'LR', 'L');
		}
		$this->fpdf->Cell(1 , 0.6, number_format($row['service_tax'],2,',','.').' %' , 1, 'LR', 'R');
		$this->fpdf->Cell(1 , 0.6, number_format($row['ppn_tax'],2,',','.').' %' , 1, 'LR', 'R');
		$this->fpdf->Cell(1.8 , 0.6, $row['payment_name'] , 1, 'LR', 'C');
		$total = $total + ($total * $row['service_tax'] / 100) + ($total * $row['ppn_tax'] / 100);
		$this->fpdf->Cell(2.5 , 0.6, 'Rp. '.number_format($total,2,',','.') , 1, 'LR', 'R');
		$grand_total = $grand_total + $total;
		
		if($this->fpdf->GetY() >= 17.5) {
			$this->pdf_footer();
			$this->fpdf->AddPage('L', 'A4');
			$this->pdf_header();
		}
	}
}

$this->fpdf->Ln();
$x = $this->fpdf->GetX();
$y = $this->fpdf->GetY();
$this->fpdf->SetXY($x, $y);
$this->fpdf->SetFont('Arial','B',7);
$this->fpdf->Cell(26.6 , 0.6, 'Total' , 1, 'LR', 'C', true);
$this->fpdf->Cell(2.5 , 0.6, 'Rp. '.number_format($grand_total,2,',','.') , 1, 'LR', 'R', true);


/* setting posisi footer 3 cm dari bawah */
$this->pdf_footer();
 
/* generate pdf jika semua konstruktor, data yang akan ditampilkan, dll sudah selesai */
$this->fpdf->Output("daily_sales.pdf","I");
?>